import '@babel/polyfill';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import gsap from 'gsap';
import * as dat from 'dat.gui';

//点材质创建星空

//场景
const scene = new THREE.Scene();

//相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(5, 3, 5);
scene.add(camera);

//渲染器
const renderer = new THREE.WebGL1Renderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

//纹理
const texture = new THREE.TextureLoader().load('./particles/9.png');

//点mesh
const bufferGeometry = new THREE.BufferGeometry();
const number = 5000;
const vertices = new Float32Array(number * 3);
const color = new Float32Array(number * 3);
for (let i = 0; i < number * 3; i++) {
    vertices[i] = Math.random() * 50 - 25;
    color[i] = Math.random();
};
bufferGeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
bufferGeometry.setAttribute('color', new THREE.BufferAttribute(color, 3));


const material = new THREE.PointsMaterial({
    size: 0.5,
    vertexColors: true,
    //map:texture,
    alphaMap: texture,
    transparent: true,
    depthWrite: false//透明贴图的透明对深度模型有影响嘛
});
const points = new THREE.Points(bufferGeometry, material);
scene.add(points);

//控制
const orbitControls = new OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;

//坐标轴
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);


//帧动画
function animate() {
    orbitControls.update();
    renderer.render(scene, camera);
    requestAnimationFrame(animate);
};
animate();

//窗口调整侦听
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});


